home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2407 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  97 lines

  1. Path: news.gate.net!pslfl2-45
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question on pointer-to-pointer-to-pointer-...
  5. Date: 21 Jan 1996 03:51:03 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4dsd77$16qo@news.gate.net>
  8. References: <4dq2ej$m3t@cssun.cs.usm.my>
  9. NNTP-Posting-Host: pslfl2-45.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4dq2ej$m3t@cssun.cs.usm.my>,
  13.    bahari@cs.usm.my (Bahari Belaton (Dr)) spake:
  14. ;Hi,
  15. ;Below is one of the question asked by my student about pointer-to-pointer in
  16. ;C - with a specific example on strtod() function. Can anyone help me explain
  17. ;this to my student. Especially on the important of pointer-to-pointer.
  18. ;
  19. ;Thanks
  20. ;
  21. ;Bahari Belaton
  22. ;
  23. ;=======================================================================
  24. ;Dear All,
  25. ;     I hope you can enlighthen me a bit.
  26. ;Query 1:
  27. ;     According to Deitel, the function prototype for strtod() is:-
  28. ;     double strtod(const char *nPtr, char **endPtr) and the function call is
  29. ;as follows :-
  30. ;     double d;
  31. ;     char *string = "51.2% are admitted";
  32. ;     char *stringPtr;
  33. ;
  34. ;     d = strtod(string, &stringPtr);
  35. ;
  36. ;The book says that &stringPtr is assigned the location of the first character
  37. ;after the converted value. How does the function do that?
  38.  
  39. If you have the varibles defined:
  40.  
  41. char *stringPtr;
  42. char *string;
  43. double d;
  44.  
  45. and call strtod:
  46.  
  47.      d = strtod(string, &stringPtr);
  48.     
  49. The function might look something like this:
  50.  
  51. double strtod(const char *s, char **ptr)
  52. {
  53. double retval;
  54.  
  55.     *ptr=s;
  56.     while(/* **ptr fits strtod()'s form */) {
  57.         /*process*/
  58.         (*ptr)++;
  59.     }
  60.     return retval;
  61. }
  62.  
  63. Now stringPtr, from the caller would hold a pointer to the value that stopped 
  64. the scan.
  65.  
  66. ;
  67. ;I'm still confused with **endPtr. Why the **endPtr(pointer to a
  68. ;pointer) is used?
  69.  
  70. Because it is designed to return two values (char * and double). It could have 
  71. returned a structure:
  72.  
  73. struct strtod_struc {
  74.     double    retval;
  75.     char *    endptr;
  76. };
  77.  
  78. To indirectly modify the pointer from the caller is a cleaner solution. You 
  79. get your double, and a pointer to continue string processing with or 
  80. determine what the hell is wrotten in Denmark (no offense to the Danish).
  81.  
  82. ;(What is the significance of using a double pointer? Why
  83. ;not just *endPtr since the function assigned the address of first
  84. ;character of the string to &stringPtr. 
  85.  
  86. There would be no sense in passing it a char *, because it would not need it. 
  87. What it needs is the address of your char * so it can modify it.
  88.  
  89. ;(what about***endPtr? How many pointers to pointer can we used?)
  90.  
  91. Never used that many levels in C and don't have the documentation. Someone 
  92. else can enlighten us on that one.
  93.  
  94. Bill
  95.  
  96. "Whatcha got on?...Your mind?"
  97.